//
//  WLLStatusPictureView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//cell 重用标识
private let WLLStatusPictureViewCellId = "WLLStatusPictureViewCellId"
//cell 的宽高
private let Margin:CGFloat = 5
private let cellWH:CGFloat = 80
class WLLStatusPictureView: UICollectionView {

    //供外界赋值的属性
        /// 配图数组
    var pictureInfo : [WLLPictureInfo]?{
        didSet{
            //测试用的
//            testLabel.text = "\(pictureInfo?.count ?? 0)"
            //得到 size
            let size = getPictureSize(pictureInfo?.count ?? 0)
       // 更新约束 因为cell 是复用的 但是pictureView的相对约束的对象没有发生改变
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            //刷新 UI
            reloadData()
        }
    }
    /**
     计算配图的 size
     */
    func getPictureSize(count:Int) -> CGSize  {
                /// 列数
        let col = count == 4 ? 2 : count>3 ? 3 : count
        /// 行数
        let row =  count == 4 ? 2 : (count - 1)/3 + 1
        //计算宽高
        let w = cellWH*CGFloat( col) + Margin*CGFloat (col - 1)
        let h = cellWH*CGFloat( row) + Margin*CGFloat (row - 1)
        
        return CGSizeMake(w, h)
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
//        super.init(frame: frame, collectionViewLayout: UICollectionViewLayout())
        //WARNING:这里是FlowLayout
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    func setupUI()  {
        backgroundColor = AdditionColor
        addSubview(testLabel)
        testLabel.snp_updateConstraints { (make) in
            make.center.equalTo(self)
        }
        
         //MARK: - 设置代理
         dataSource = self
        
        //注册
        registerClass(WLLStatusPictureViewCell.self , forCellWithReuseIdentifier: WLLStatusPictureViewCellId)
        
        //设置 layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(cellWH, cellWH)
        
        layout.minimumLineSpacing = Margin
        layout.minimumInteritemSpacing = Margin
        
    }
    
    
    
    
     //MARK: - 懒加载控件
    private lazy var testLabel : UILabel = UILabel(textColor: ThemeColor, textFont: 38)
}


 //MARK: - UICollectionViewDataSource
extension WLLStatusPictureView : UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        printLog(pictureInfo?.count)
        return pictureInfo?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WLLStatusPictureViewCellId, forIndexPath: indexPath) as! WLLStatusPictureViewCell
        
        cell.picUrl = pictureInfo![indexPath.item]
        
        return cell
    }
    
}

 //MARK: - 自定义 cell
class WLLStatusPictureViewCell : UICollectionViewCell{
    
    //供外界赋值
    var picUrl : WLLPictureInfo?{
        didSet{
            bgImageView.ll_setImageWithString(picUrl?.thumbnail_pic, placeholderImageName: "weibo_placeholder")
            // 判断图片的后缀是否是.gif
            if let g = picUrl?.thumbnail_pic where g.hasSuffix(".gif") {
                //如果是 gif 则显示
                gifImageView.hidden = false
            }else{
                //隐藏 gif
                gifImageView.hidden = true
            }
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     //MARK: - 设置视图
    func setupUI()  {
        //背景色
        backgroundColor = PictureViewCellColor
        //添加控件...这里注意要添加到 contentView 上
        //01
       contentView.addSubview(bgImageView)
        //02
        contentView.addSubview(gifImageView)
        //添加约束
        //01
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        //02
        gifImageView.snp_makeConstraints { (make) in
            make.trailing.bottom.equalTo(contentView)
        }
    }
    
      //MARK: - 懒加载控件
    lazy var bgImageView : UIImageView = {
        let img = UIImageView(imageName: "weibo_placeholder")
        img.contentMode  = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
     /// gif图片
    lazy var gifImageView : UIImageView = UIImageView(imageName: "timeline_image_gif")
}




